package com.ruoyi.project.blog.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.blog.domain.BSiteClass;
import com.ruoyi.project.blog.service.IBSiteClassService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 网址分类Controller
 * 
 * @author ruoyi
 * @date 2023-10-10
 */
@RestController
@RequestMapping("/webManage/siteClass")
public class BSiteClassController extends BaseController
{
    @Autowired
    private IBSiteClassService bSiteClassService;

    /**
     * 查询网址分类列表
     */
    @GetMapping("/list")
    @Log(title = "导航管理", businessType = BusinessType.OTHER)
    public TableDataInfo list(BSiteClass bSiteClass)
    {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if(!admin){
            bSiteClass.setUserId(SecurityUtils.getUserId().toString());
        }
        startPage();
        List<BSiteClass> list = bSiteClassService.selectBSiteClassList(bSiteClass);
        return getDataTable(list);
    }

    /**
     * 导出网址分类列表
     */
    @Log(title = "导航管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BSiteClass bSiteClass)
    {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if(!admin){
            bSiteClass.setUserId(SecurityUtils.getUserId().toString());
        }
        List<BSiteClass> list = bSiteClassService.selectBSiteClassList(bSiteClass);
        ExcelUtil<BSiteClass> util = new ExcelUtil<BSiteClass>(BSiteClass.class);
        util.exportExcel(response, list, "网址分类数据");
    }

    /**
     * 获取网址分类详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bSiteClassService.selectBSiteClassById(id));
    }

    /**
     * 新增网址分类
     */
    @Log(title = "导航管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BSiteClass bSiteClass)
    {
        bSiteClass.setUserId(SecurityUtils.getUserId().toString());
        return toAjax(bSiteClassService.insertBSiteClass(bSiteClass));
    }

    /**
     * 修改网址分类
     */
    @Log(title = "导航管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BSiteClass bSiteClass)
    {
        return toAjax(bSiteClassService.updateBSiteClass(bSiteClass));
    }

    /**
     * 删除网址分类
     */
    @Log(title = "导航管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bSiteClassService.deleteBSiteClassByIds(ids));
    }
}
